package com.codeczx.searchdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.LoginFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    LinearLayout llEt;
    LinearLayout llMain;
    RecyclerView rv;
    RVAdapter adapter;
    EditText et;
    Button btn;
    PopupWindow popupWindow;
    List<String> strs;
    List<String> tmpStrs;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = (RecyclerView) findViewById(R.id.rv);
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        llEt = (LinearLayout) findViewById(R.id.ll_et);
        llMain = (LinearLayout) findViewById(R.id.ll_main);

        btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange: btn"+hasFocus);
            }
        });

        llMain.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange: llmain"+hasFocus);
            }
        });

        llMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llMain.setFocusable(true);
            }
        });

        strs = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strs.add("我是第"+i+"个item");
        }

        adapter = new RVAdapter(strs);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);

        et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()!=0){
                    for (int i = 0; i < strs.size(); i++) {
                        if (strs.get(i).contains(s)) {
                            tmpStrs.add(strs.get(i));
                        }
                    }
                    adapter.updateData(tmpStrs);
                }else{
                    adapter.updateData(strs);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange: et"+hasFocus);
                if(hasFocus){
                    if(et.getText().length()==0){
                        rv.setVisibility(View.VISIBLE);
                    }
                }else{
                    rv.setVisibility(View.GONE);
                }
            }
        });
    }
}
